/*
 * Created on Jan 10, 2004
 */
package org.vectrics.recruiter.user;


import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.vectrics.SystemException;
import org.vectrics.common.db.RecordObject;
import org.vectrics.common.db.Voidable;
//import org.vectrics.hibernate.HibernateSessionStore;
import org.vectrics.location.Location;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.JobFunction;


/**
 * @author mmoore
 */
public class JobAgent implements RecordObject, Serializable, Voidable {
	public static final long serialVersionUID = 63473464;
	
	public static int SEARCH_PHRASE_AND = 0;
	public static int SEARCH_PHRASE_OR = 1;
	private Long id = null;
	private Integer daysBack = new Integer(7);
	private CandidateInfo candidateInfo = null;
	private String searchPhrase = "";
	private Integer travelPercent = new Integer(100);
	private Set locations = new HashSet();
	private Integer searchPhraseCondition = new Integer(JobAgent.SEARCH_PHRASE_AND);
	private boolean active = true;
	private Date dateLastServiced = null;
	private Set jobFunctions = new HashSet();
	private String name = "Job Agent";
	private Date triggerDate = new Date();
	private Date dateCreated = new Date();
	private Integer sendFailureCount = new Integer(0);
	private boolean voided = false; 
	
	/*
	 * 
	 */
	public JobAgent() {
		super();
	}

	/*
	 * @return
	 */
	public boolean getActive() {
		return active;
	}

	/*
	 * @param active
	 */
	public void setActive(boolean active) {
		this.active = active;
	}

	/*
	 * @return
	 */
	public Integer getDaysBack() {
		return daysBack;
	}

	/**
	 * @param daysBack
	 */
	public void setDaysBack(Integer daysBack) {
		this.daysBack = daysBack;
	}

	/**
	 * @return
	 */
	public String getSearchPhrase() {
		return searchPhrase;
	}

	/**
	 * @param searchPhrase
	 */
	public void setSearchPhrase(String searchPhrase) {
		this.searchPhrase = searchPhrase;
	}

	/**
	 * @return
	 */
	public Integer getTravelPercent() {
		return travelPercent;
	}

	/**
	 * @param travelPercent
	 */
	public void setTravelPercent(Integer travelPercent) {
		this.travelPercent = travelPercent;
	}

	/**
	 * @param locations The locations to set.
	 */
	public void setLocations(Set locations) {
		this.locations = locations;
	}
	
	public Set getLocations() {
		return(this.locations);		
	}
	
	/**
	 * @return Returns the searchPhraseCondition.
	 */
	public Integer getSearchPhraseCondition() {
		return searchPhraseCondition;
	}
	/**
	 * @param searchPhraseCondition The searchPhraseCondition to set.
	 */
	public void setSearchPhraseCondition(Integer searchPhraseCondition) {
		this.searchPhraseCondition = searchPhraseCondition;
	}
	/**
	 * @return Returns the id.
	 */
	public Long getId() {
		return id;
	}
	/**
	 * @param id The id to set.
	 */
	public void setId(Long id) {
		this.id = id;
	}
	/**
	 * @return Returns the dateLastServiced.
	 */
	public Date getDateLastServiced() {
		return dateLastServiced;
	}
	/**
	 * @param dateLastServiced The dateLastServiced to set.
	 */
	public void setDateLastServiced(Date dateLastServiced) {
		this.dateLastServiced = dateLastServiced;
	}
	
	public JobAgentLocation addLocation(Location location) {
		if (this.getId() == null)
			throw new SystemException("JobAgent should be saved before adding locations");
		if (location.getId() == null)
			throw new SystemException("Location should be saved before adding it to job agent");
		
		JobAgentLocation jobAgentLocation = new JobAgentLocation();
		jobAgentLocation.setAgent(this);
		jobAgentLocation.setLocation(location);
		RecruitServiceLocator.getRecruitUserService().updateJobAgentLocation(jobAgentLocation);
		this.locations.add(jobAgentLocation);
		return(jobAgentLocation);
	}
	
	public JobAgentLocation findLocation(Long locationId) {
		JobAgentLocation found = null;
		Iterator iter = locations.iterator();
		while ((found == null) && (iter.hasNext())) {
			JobAgentLocation location = (JobAgentLocation)iter.next();
			if ((location != null) && (location.getId() != null)) {
				if (location.getLocation().getId().equals(locationId)) {
					found = location;
				}
			}
		}
		return(found);
	}
	
	public void removeLocation(Location location) {
		Long locationId = location.getId();
		JobAgentLocation agentLocation = findLocation(locationId);
		if (agentLocation != null) {
			this.locations.remove(agentLocation);
		}
	}
	
	/**
	 * @return Returns the jobFunctions.
	 */
	public Set getJobFunctions() {
		return jobFunctions;
	}
	/**
	 * @param jobFunctions The jobFunctions to set.
	 */
	public void setJobFunctions(Set jobFunctions) {
		this.jobFunctions = jobFunctions;
	}
	
	public void addJobFunction(JobFunction jobFunction) {
    	JobAgentFunction jobAgentFunction = new JobAgentFunction();
	    if (jobFunction != null) {
	    	jobAgentFunction.setAgent(this);
	    	jobAgentFunction.setJobFunction(jobFunction);
			if (jobFunction == null)
				throw new SystemException("jobFunction is null");
			if (jobFunction.getCode() == null)
				throw new SystemException("jobFunction code is null");
			if (this.findJobFunction(jobFunction.getCode()) == null)
			    this.jobFunctions.add(jobAgentFunction);
	    }
	}
	
	public JobAgentFunction findJobFunction(String jobFunctionCode) {
		JobAgentFunction found = null;
		Iterator iter = this.getJobFunctions().iterator();
		while ((found == null) && (iter.hasNext())) {
			JobAgentFunction function = (JobAgentFunction)iter.next();
			if ((function != null) && (function.getJobFunction().getCode() != null)) {
				if (function.getJobFunction().getCode().equals(jobFunctionCode)) {
					found = function;
				}
			}
		}
		return(found);
	}
	
	public void removeJobFunction(JobFunction jobFunction) {
		JobAgentFunction function = this.findJobFunction(jobFunction.getCode());
		if (function != null) {
			this.jobFunctions.remove(function);
		}
	}

	
	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * @return Returns the triggerDate.
	 */
	public Date getTriggerDate() {
		return triggerDate;
	}

	/**
	 * @param triggerDate The triggerDate to set.
	 */
	public void setTriggerDate(Date triggerDate) {
		this.triggerDate = triggerDate;
	}


	/**
	 * @return Returns the candidateInfo.
	 */
	public CandidateInfo getCandidateInfo() {
		return candidateInfo;
	}

	/**
	 * @param candidateInfo The candidateInfo to set.
	 */
	public void setCandidateInfo(CandidateInfo candidateInfo) {
		this.candidateInfo = candidateInfo;
	}

    /**
     * @return Returns the dateCreated.
     */
    public Date getDateCreated() {
        return dateCreated;
    }
    /**
     * @param dateCreated The dateCreated to set.
     */
    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }
    
    
    /**
     * @return Returns the sendFailureCount.
     */
    public Integer getSendFailureCount() {
        return sendFailureCount;
    }
    /**
     * @param sendFailureCount The sendFailureCount to set.
     */
    public void setSendFailureCount(Integer sendFailureCount) {
        this.sendFailureCount = sendFailureCount;
    }
    
    
    /**
     * @return Returns the voided.
     */
    public boolean getVoided() {
        return voided;
    }
    /**
     * @param voided The voided to set.
     */
    public void setVoided(boolean voided) {
        this.voided = voided;
    }
}
